package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QyiSupplierMapper;
import com.ruoyi.system.domain.QyiSupplier;
import com.ruoyi.system.service.IQyiSupplierService;
import com.ruoyi.common.core.text.Convert;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

/**
 * 供应商Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class QyiSupplierServiceImpl implements IQyiSupplierService 
{
    @Autowired
    private QyiSupplierMapper qyiSupplierMapper;

    /**
     * 查询供应商
     * 
     * @param supplierId 供应商主键
     * @return 供应商
     */
    @Override
    public QyiSupplier selectQyiSupplierBySupplierId(Long supplierId)
    {
        return qyiSupplierMapper.selectQyiSupplierBySupplierId(supplierId);
    }

    /**
     * 查询供应商列表
     * 
     * @param qyiSupplier 供应商
     * @return 供应商
     */
    @Override
    public List<QyiSupplier> selectQyiSupplierList(QyiSupplier qyiSupplier)
    {
        return qyiSupplierMapper.selectQyiSupplierList(qyiSupplier);
    }

    /**
     * 新增供应商
     * 
     * @param qyiSupplier 供应商
     * @return 结果
     */
    @Override
    public int insertQyiSupplier(QyiSupplier qyiSupplier)
    {
        qyiSupplier.setCreateTime(DateUtils.getNowDate());
        return qyiSupplierMapper.insertQyiSupplier(qyiSupplier);
    }

    /**
     * 修改供应商
     * 
     * @param qyiSupplier 供应商
     * @return 结果
     */
    @Override
    public int updateQyiSupplier(QyiSupplier qyiSupplier)
    {
        qyiSupplier.setUpdateTime(DateUtils.getNowDate());
        return qyiSupplierMapper.updateQyiSupplier(qyiSupplier);
    }

    /**
     * 批量删除供应商
     * 
     * @param supplierIds 需要删除的供应商主键
     * @return 结果
     */
    @Override
    public int deleteQyiSupplierBySupplierIds(String supplierIds)
    {
        return qyiSupplierMapper.deleteQyiSupplierBySupplierIds(Convert.toStrArray(supplierIds));
    }

    /**
     * 删除供应商信息
     * 
     * @param supplierId 供应商主键
     * @return 结果
     */
    @Override
    public int deleteQyiSupplierBySupplierId(Long supplierId)
    {
        return qyiSupplierMapper.deleteQyiSupplierBySupplierId(supplierId);
    }

    @Override
    public List<QyiSupplier> selectQyiSupplierByGoodsId(Long goodsId) {
        List<QyiSupplier> goodsSuppliers = qyiSupplierMapper.selectQyiSupplierByGoodsId(goodsId);
        List<QyiSupplier> suppliers = selectSupplierAll();
        for (QyiSupplier supplier : suppliers)
        {
            for (QyiSupplier goods : goodsSuppliers)
            {
                Long supplierId = supplier.getSupplierId().longValue();
                Long goo = goods.getSupplierId().longValue();
                if (goo == supplierId)
                {
                    supplier.setFlag(true);
                    break;
                }
            }
        }
        return suppliers;
    }

    @Override
    public List<QyiSupplier> selectSupplierAll() {
        return qyiSupplierMapper.selectSupplierAll();
    }
}
